<?php
/**
 * Created by PhpStorm.
 * User: 12133
 * Date: 2025/1/2
 * Time: 15:12
 */

namespace app\controller\home;

use app\model\home\Content;
use app\model\home\Nav;
use app\model\home\Website;

class Info extends Base
{
    public function web($id = 0){
        $post = input('post.');
        if (!empty($id))
            $info = Website::find($id);
        if (!empty($post)) {
            if (!empty($info->id)){
                $info->save($post);
                $msg = '修改网站成功';
            }else{
                Website::create($post);
                $msg = '添加网站成功';
            }
            return $this->success('/home/list/web',$msg);
        }
        return $this->view('/home/info/web',['info' => $info ?? []]);
    }

    public function nav($id = 0){
        $post = input('post.');
        if (!empty($id))
            $info = Nav::find($id);
        if (!empty($post)) {
            if (!empty($info->id)) {
                $info->save($post);
                $msg = '修改成功';
            } else {
                global $wid;
                $post['website_id'] = $wid;
                $post['sort'] = Nav::count();
                Nav::create($post);
                $msg = '添加成功';
            }
            return $this->success('/home/list/nav',$msg);
        }
        $list = Nav::order('sort ASC')->select();
        return $this->view('/home/info/nav',['info' => $info ?? [],'list' => $list]);
    }

    public function info($id = 0){
        $post = input('post.');
        if (!empty($post))
            $info = Content::create($post);
        if (!empty($id))
            $info = Content::info($id);
        return $this->view('/home/info/info',['info' => $info ?? []]);
    }
}